#include "stdafx.h"

#include "oil_model_editor.h"
#include "oil_model.h"

using namespace std;
static const QColor indexUsedColor = QColor(167, 211, 92);

OilModelEitor::OilModelEitor(QWeakPointer<Common> _common) : common(_common)
	//: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);
	connect(ui.bAdd, SIGNAL(clicked), this, SLOT(on_bAdd_clicked));
	connect(ui.bRemove, SIGNAL(clicked), this, SLOT(on_bRemove_clicked));
	connect(ui.listOfModels, SIGNAL(currentRowChanged(int)), this, SLOT(displayOilModel(int)));
	connect(ui.bUse, SIGNAL(clicked), this, SLOT(on_bUse_clicked));
}

OilModelEitor::~OilModelEitor(){
}

void OilModelEitor::on_bAdd_clicked(){
	bool ok;
	QString name = QInputDialog::getText(this, tr("Creating oil model..."),
		tr("Enter a model name"), QLineEdit::Normal,tr("oil_model"), &ok);
	if(ok){
		if(name.isEmpty()){
			QMessageBox::critical(0, tr("Error"), tr("Empty name is not valid!"), QMessageBox::Ok);	
			return;
		}
		for (int i = 0; i < ui.listOfModels->count(); i++)
			if(ui.listOfModels->item(i)->text() == name){
				QMessageBox::critical(0, tr("Error"), tr("That name already exists!"), QMessageBox::Ok);	
				return;
			}
		oilModelList.push_back(QSharedPointer<OilModel>(new OilModel(common, name)));
		ui.listOfModels->addItem(name);
		ui.listOfModels->setCurrentRow(ui.listOfModels->count() - 1);
		displayOilModel(ui.listOfModels->currentRow());	
		//common.data()->log("Oil model: '" + name + "' has been added");
	}
	
}

void OilModelEitor::on_bRemove_clicked(){
	if (ui.listOfModels->currentRow() == -1){
		QMessageBox::information(0, tr("Removing oil model..."), tr("Model is not selected!"), QMessageBox::Ok);	
		return;
	}
	if(QMessageBox::question(0, tr("Removing oil model..."), QString(tr("Remove the oil model: '%1'?")).arg(ui.listOfModels->currentItem()->text()),
		QMessageBox::Yes |QMessageBox::No) == QMessageBox::Yes)
	{	
		oilModelList.erase(oilModelList.begin() + ui.listOfModels->currentRow());
		//QString name = ui.listOfModels->currentItem()->text();
		delete ui.listOfModels->takeItem(ui.listOfModels->currentRow());
		displayOilModel(ui.listOfModels->currentRow());
		//common.data()->log("Oil model: '" + name + "' has been removed");
	}
}

void OilModelEitor::on_bUse_clicked(){
	for(int i = 0; i < ui.listOfModels->count(); i++){
		if (i == ui.listOfModels->currentRow()){
			ui.listOfModels->item(i)->setBackgroundColor(indexUsedColor);
		}else{
			ui.listOfModels->item(i)->setBackgroundColor(Qt::white);
		}
	}
}

void OilModelEitor::displayOilModel(int index){
	if (index != -1) setModel(oilModelList[index].data()->model);
	else setModel(QSharedPointer<ParametersModel>(new ParametersModel()));
	for(int i = 3; i <= 7; i++)
		ui.trParameters->hideColumn(i);
}

void OilModelEitor::setModel(QSharedPointer<ParametersModel> _model){
	ui.trParameters->setModel(_model.data());
	ui.trParameters->setItemDelegate(new Delegate(common.data()->getParametersWidget()));
	ui.trParameters->setStyleSheet("alternate-background-color: rgb(190, 234, 235)");
	for(int i = 3; i <= 6; i++)
		ui.trParameters->hideColumn(i);
	ui.trParameters->header()->setStretchLastSection(false);
	ui.trParameters->header()->setResizeMode(1, QHeaderView::Stretch);   
	ui.trParameters->setColumnWidth(0, 120);
	ui.trParameters->setColumnWidth(2, 40);
}

void OilModelEitor::setData(vector<QSharedPointer<OilModel>> _oilModelList, int _index){
	oilModelList = _oilModelList;
	for (int i = 0; i < oilModelList.size(); i++){
		ui.listOfModels->addItem(oilModelList[i]->getName());
	}
	if (_index >= 0 && _index < ui.listOfModels->count())
		ui.listOfModels->item(_index)->setBackgroundColor(QColor(167, 211, 92));
}

vector<QSharedPointer<OilModel>> OilModelEitor::getData(){
	return oilModelList;
}

int OilModelEitor::getIndex(){
	int index_of_model_used;
	for(index_of_model_used = 0; index_of_model_used < ui.listOfModels->count(); index_of_model_used++)
		if (ui.listOfModels->item(index_of_model_used)->backgroundColor() == indexUsedColor)
			break;
	if (index_of_model_used == ui.listOfModels->count()) index_of_model_used = -1;
	return index_of_model_used;
}
